using System;
using System.IO;
using System.Web;

namespace DnnMvcBridge.Web
{
	public class CaptureContext : IDisposable
	{
		private HttpContextBase Context { get; set; }
		private TextWriter OriginalOutput { get; set; }
		public StringWriter CaptureOutput { get; private set; }

		public CaptureContext(HttpContextBase context)
		{
			CaptureOutput = new StringWriter();
			OriginalOutput = context.Response.Output;
			this.Context = context;

			context.Response.Output = CaptureOutput;
		}

		public string GetOutput()
		{
			var result = CaptureOutput.GetStringBuilder().ToString();

			return result;
		}
		public void Dispose()
		{
			Context.Response.Output = OriginalOutput;
			CaptureOutput.Dispose();
		}
	}
}
